package com.ss.web.tagmark.init;

import java.beans.PropertyEditorSupport;
import java.text.SimpleDateFormat;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CustomDateEditor extends PropertyEditorSupport {
	protected Logger logger = LoggerFactory.getLogger("sys");
	private static final String[] patterns = { "yyyy-MM-dd HH:mm", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH", "yyyy-MM-dd",
			"yyyy-MM" };
	private final SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

	public String getAsText() {
		if (getValue() != null) {
			return sf.format(getValue());
		}
		return "";
	}

	public void setAsText(String text) throws java.lang.IllegalArgumentException {
		if (StringUtils.isBlank(text)) {
			return;
		}
		try {
			setValue(DateUtils.parseDate(text.trim(), patterns));
		} catch (Exception e) {
			logger.debug("springmvc进入Controller之前类型转换失败！" + e, e);
		}
	}
}
